package org.cytoscape.myapp.cytoscape_app_v1.internal.model.attributes;

import java.util.Collection;
import java.util.HashMap;
import java.util.Set;

import org.cytoscape.application.CyApplicationManager;
import org.cytoscape.model.CyColumn;
import org.cytoscape.model.CyNetwork;
import org.cytoscape.model.CyNode;
import org.cytoscape.myapp.cytoscape_app_v1.internal.metrics.domain.User;

public class AttributeMapping {
	
	public HashMap<String, Attribute> columns;
	public CyApplicationManager applicationManager;
	
	public AttributeMapping(CyApplicationManager applicationManager){
		this.columns = new HashMap<String, Attribute>();
		this.applicationManager = applicationManager;
		this.setValuesColumnsNode(applicationManager);
	}
	
	public CyNetwork getCyNetwork(){
		return applicationManager.getCurrentNetwork();
	}
	
	public void setValuesColumnsNode(CyApplicationManager applicationManager){
		
		Collection<CyColumn> cyColumns = applicationManager.getCurrentNetwork().getDefaultNodeTable().getColumns();
		
		for (CyColumn cyColumn : cyColumns) {
			
			if (cyColumn.getListElementType() != null){
				Class<?> typeClass = cyColumn.getListElementType();
				this.columns.put(cyColumn.getName(), 
						new AttributeList(typeClass, cyColumn.getName()));
			}
			else {
				Class<?> typeClass = cyColumn.getType();
				this.columns.put(cyColumn.getName(), 
						new Attribute(typeClass, cyColumn.getName()));
			}

		}
	}
	
public void setValuesColumnsEdge(CyApplicationManager applicationManager){
		
		Collection<CyColumn> cyColumns = applicationManager.getCurrentNetwork().getDefaultEdgeTable().getColumns();
		
		for (CyColumn cyColumn : cyColumns) {
			
			if (cyColumn.getListElementType() != null){
				Class<?> typeClass = cyColumn.getListElementType();
				this.columns.put(cyColumn.getName(), 
						new AttributeList(typeClass, cyColumn.getName()));
			}
			else {
				Class<?> typeClass = cyColumn.getType();
				this.columns.put(cyColumn.getName(), 
						new Attribute(typeClass, cyColumn.getName()));
			}

		}
	}
	
	public Object getValueByAttribute(String name, CyNode node){
		return this.columns.get(name).getValuesCyNode(this.getCyNetwork(), node);
	}
	
	public User getUserByCyNode(CyNode node){
		User usuario = new User();
		Set<String> keys = this.columns.keySet();
		for (String name : keys) {
			usuario.setAttribute(name, this.getValueByAttribute(name, node));
		}
		
		return usuario;
	}

	
}
